package com.bestcem.xm.wallet.grpc.client.converter;

import com.bestcem.xm.deliver.grpc.v1.services.GetSmsBalanceByOrgIdResponse;
import com.bestcem.xm.deliver.grpc.v1.services.SMSBalance;
import com.bestcem.xm.wallet.constant.CommonConstant;
import com.bestcem.xm.wallet.grpc.client.dto.SmsBalanceDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * 短信服务转换类
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/11 13:30
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class WalletSmsConverter {

    /**
     * response to SmsBalanceDTO
     *
     * @param response 短信余额信息
     * @return com.bestcem.xm.user.grpc.client.dto.SmsTaskInfoDTO
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/20 20:59
     */
    public static SmsBalanceDTO responseToSmsTaskInfoDto(GetSmsBalanceByOrgIdResponse response) {
        SmsBalanceDTO smsBalanceDTO = new SmsBalanceDTO();
        if (Objects.isNull(response)) {
            return smsBalanceDTO;
        }
        SMSBalance smsBalance = response.getBalance();
        smsBalanceDTO.setId(smsBalance.getId())
                .setOrgId(smsBalance.getOrgId())
                .setCode(smsBalance.getCode())
                .setBalance(Math.floorDiv(smsBalance.getBalance(), CommonConstant.SMS_PER_BALANCE))
                .setSuccessNumber(smsBalance.getSuccessNumber())
                .setSignature(smsBalance.getSignature())
                .setVersion(smsBalance.getVersion());
        return smsBalanceDTO;
    }

}
